//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by LL.F on 16/7/18.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
     *
     *  UIGestureRecognizer 手势识别器 作为所有手势的基类(父类)
     *
     */
    
//     1.轻拍: Tap UITapGestureRecognizer
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击几次触发:
    tap.numberOfTapsRequired = 1;//(默认为1)
    // 使用几个手指点击触发:(键盘上option = alt)
    tap.numberOfTouchesRequired = 1;//(默认为1)
    [self.view addGestureRecognizer:tap];
    
    // 2.长按 UILongPressGestureRecognizer
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 最小按压持续时间, 即长按几秒后开始 默认是0.5秒
    longPress.minimumPressDuration = 2;
    // 允许最大移动距离, 默认是10像素
    longPress.allowableMovement = 50;
    [self.view addGestureRecognizer:longPress];
//
////    左右与上下有冲突
//    
//    
//    // 3.轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction)];
    // 设置轻扫方向(枚举:默认为0 从左向右 1从右向左 2从下向上 3从上到下)
    // direction可以设置多个方向, 前提是多个方向所触发的行为要相同
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;// 两个方向都可以, 即指定多个方向
    [self.view addGestureRecognizer:swipe];
//
//    // 练习: 实现ImageView添加不同方向轻扫换图
//    // 解题思路:
//    // 1.不同方向写不同的手势,就是两个方向写两个手势, 手势方法的实现中实现图片的切换
//    // 2.创建一个轻扫手势, 手势的方向设置为两个, 手势的实现方法中,用BOOL标记实现图片的切换
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imageView.tag = 1000;
    imageView.image = [UIImage imageNamed:@"guidePage_01"];
    UISwipeGestureRecognizer *swipeImage = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImageAction:)];
    swipeImage.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:swipeImage];
    [self.view addSubview:imageView];
    // 按位枚举:
    // 例子: UISwipeGestureRecognizerDirectionRight = 1 << 0,
    //      UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
    //      UISwipeGestureRecognizerDirectionUp    = 1 << 2,
    //      UISwipeGestureRecognizerDirectionDown  = 1 << 3
    //  0,1,2,3 分别表示从右到左分别在第几位 因此从左到右排列分别为: 下上左右
//    //
//    
    // 4.平移 Pan
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    
    
    // 5.捏合
//    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
//    [imageView addGestureRecognizer:pinch];
    
    // 6.旋转
//    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
//    [imageView addGestureRecognizer:rotation];
    // 7.屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePan)];
    [imageView addGestureRecognizer:screenEdgePan];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    
}

- (void)screenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
    NSLog(@"屏幕边缘");
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
//    转换中，根据上次转换继续改变
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    rotation.rotation = 1;//旋转的弧度置为0
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"%f",pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 10;
}
//平移
- (void)panAction:(UIPanGestureRecognizer *)pan
{
//    NSLog(@"平移");
    if (pan.state == UIGestureRecognizerStateBegan) {
        //平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged)
    {
//     平移中
        CGPoint point = [pan translationInView:pan.view];//平移手势，根据其他view上移动的过程，得到最終点
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);//根据point使pan.view移动
        [pan setTranslation:CGPointZero inView:pan.view];//pan移动的点置空
    } else if(pan.state == UIGestureRecognizerStateEnded){
        pan.view.transform = CGAffineTransformIdentity;//平移结束
    }
}

#pragma mark --轻拍方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{

    NSLog(@"轻拍");
    
}
#pragma mark --长按方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{

    if (longPress.state == UIGestureRecognizerStateBegan) {
         NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
    
        NSLog(@"长按结束");
    }
   
}
#pragma mark --轻扫方法
- (void)swipeAction
{

    NSLog(@"别扫我");
}
// 轻扫改变图片方法
- (void)swipeImageAction:(UISwipeGestureRecognizer *)swipe
{
//    NSLog(@"%ld", swipe.direction);// 结果为3 二进制数0011的转换
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView = [self.view viewWithTag:1000];
//    if (_swipeToLeft == YES) {
//       imageView.image = [UIImage imageNamed:@"guidePage_01"];
//        _swipeToLeft = NO;
//    } else {
//    
//        imageView.image = [UIImage imageNamed:@"guidePage_02"];
//        _swipeToLeft = YES;
//    }
    
    //
    if (swipe.direction & UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"aaaaaaa");// & 是包含的含义
        NSLog(@"bbbb");
    }
    

}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
